package com.enniswu.weitter.data.remote;

import android.support.annotation.Nullable;

import com.enniswu.weitter.base.BaseModel;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.entity.JsonBean;
import com.enniswu.weitter.entity.Post;
import com.enniswu.weitter.network.HttpClient;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PostLoader extends BaseModel {

    /**
     * @param context  内容
     * @param type     类型，0公共，1个人动态
     * @param targetId 0帖子，帖子id评论
     */
    public void post(String context, int type, int targetId, Callback<Integer> callback) {
        Map<String, String> params = new HashMap<>(4, 1);
        params.put("context", context);
        params.put("type", String.valueOf(type == 0 ? 3 : 5));
        params.put("targetId", String.valueOf(targetId));
        params.put("postType", String.valueOf(type == 1 ? 3 : 1));
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.POST_ADD, params, new HttpCallbackImpl<>(callback,
                        JsonBean.TypeStore.intType));
    }

    public void getPost(int id, final int postType, final FileLoader.AlbumCallback callback) {
        final Map<String, String> params = new HashMap<>(2, 1);
        params.put("id", String.valueOf(id));
        params.put("postType", String.valueOf(postType));
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.POST_GET, params, new HttpCallbackImpl<Post>(callback,
                        JsonBean.TypeStore.postType) {
                    @Override
                    public void onSuccess(String json) {
                        JsonBean<Post> jsonBean = new Gson().fromJson(json, JsonBean.TypeStore.postType);
                        final Post post = jsonBean.getT();
                        if (post == null) {
                            callback.onFailure(HttpClient.getErrorMessage());
                            return;
                        }
                        post.setUserId(jsonBean.getInt());
                        post.setType(post.getType() == 5 ? 1 : 0);
                        if (post.getAlbumCount() == 0) {
                            callback.onSuccess(post);
                            return;
                        }
                        new FileLoader().downloadAlbum(post.getId(), callback);
                    }
                });
    }

    /**
     * @param id       帖子id
     * @param postType 0帖子，1评论
     */
    public void getPost(int id, int postType, final Callback<Post> callback) {
        Map<String, String> params = new HashMap<>(2, 1);
        params.put("id", String.valueOf(id));
        params.put("postType", String.valueOf(postType));
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.POST_GET, params, new HttpCallbackImpl<Post>(callback,
                        JsonBean.TypeStore.postType) {
                    @Override
                    public void onSuccess(String json) {
                        JsonBean<Post> jsonBean = new Gson().fromJson(json, JsonBean.TypeStore.postType);
                        final Post post = jsonBean.getT();
                        if (post == null) {
                            callback.onFailure(HttpClient.getErrorMessage());
                            return;
                        }
                        post.setUserId(jsonBean.getInt());
                        post.setType(post.getType() == 5 ? 1 : 0);
                        if (post.getAlbumCount() == 0) {
                            callback.onSuccess(post);
                            return;
                        }
                        new FileLoader().downloadAlbum(post.getId(), new FileLoader.AlbumCallback() {
                            @Override
                            public void onLoadSuccess(@Nullable List<String> url) {
                                post.setAlbum(url);
                                callback.onSuccess(post);
                            }

                            @Override
                            public void onFailure(String msg) {
                                callback.onFailure(msg);
                            }

                            @Override
                            public void onSuccess(@Nullable Post post) {

                            }
                        });
                    }
                });
    }

    /**
     * 0+0=公共帖子，0+1=关注帖子，0+用户id=某用户帖子，1+帖子id=某帖子评论
     *
     * @param type        0动态，1评论
     * @param orientation 0公共，1关注，用户id，帖子id
     * @param offset      偏移量
     * @param num         数量
     */
    public void getPostIds(int type, int orientation, int offset, int num, Callback<
            List<Integer>> callback) {
        Map<String, String> params = new HashMap<>(4, 1);
        String url = HttpClient.URLStore.POST_LIST_PUBLIC;
        if (type == 0) {
            if (orientation == 0) {
                params.put("type", String.valueOf(3));
                params.put("postType", String.valueOf(0));
            } else if (orientation == 1) {
                params.put("type", String.valueOf(5));
                params.put("postType", String.valueOf(3));
            } else {
                url = HttpClient.URLStore.POST_LIST_USER;
                params.put("userId", String.valueOf(orientation));
                params.put("postType", String.valueOf(0));
            }
        } else {
            url = HttpClient.URLStore.POST_LIST_COMMENT;
            params.put("targetId", String.valueOf(orientation));
        }
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                url, params, new HttpCallbackImpl<>(callback, JsonBean.TypeStore.intListType));
    }
}
